import fly from '../request'
import { isTest, baseHost } from '../config'
import CryptoJS from 'crypto-js'
import tools from '../tools'
import dayjs from 'dayjs'

let taizhouPost = (api, params, hostList, key, sid) => {
	let host = tools.chooseHost(hostList)
	let timestamp = dayjs().format('YYYYMMDDHHmmss')
	if (!sid) {
		let hostcode = '47258258'
		let random = Math.floor(Math.random() * 1000)
		if (random < 100) {
			if (random < 10) {
				random = '00' + random
			} else {
				random = '0' + random
			}
		}
		sid = hostcode + timestamp + random
	}

	let strArray = []

	objToStr(params)

	function objToStr(params, parent) {
		if (typeof params !== 'object') {
			strArray.push(`${parent}=${params}`)
		} else {
			let arrArr = Object.entries(params)
			arrArr.forEach((item, index) => {
				if (item[1]) {
					if (typeof item[1] === 'object') {
						if (Array.isArray(item[1])) {
							item[1].forEach((arrItem, arrIndex) => {
								if (parent) {
									objToStr(arrItem, `${parent}.${item[0]}[${arrIndex}]`)
								} else {
									objToStr(arrItem, `${item[0]}[${arrIndex}]`)
								}
							})
						} else {
							if (parent) {
								objToStr(item[1], `${parent}.${item[0]}`)
							} else {
								objToStr(item[1], item[0])
							}
						}
					} else {
						if (parent) {
							strArray.push(`${parent}.${item[0]}=${item[1]}`)
						} else {
							strArray.push(`${item[0]}=${item[1]}`)
						}
					}
				}
			})
		}
	}
	let sortStrArray = strArray.sort()

	let data = sortStrArray.join('&')
	let finStr = data + '&key=' + key
	let md5Data = CryptoJS.MD5(finStr)
		.toString()
		.toUpperCase()
	let sign = CryptoJS.HmacSHA256(md5Data, key).toString()
	let paramsMain = {
		body: {},
		header: {
			sid: sid,
			sign: sign,
			timestamp: timestamp
		},
		tail: {}
	}
	paramsMain.body = params
	return new Promise((resolve, reject) => {
		fly
			.post(host + api, paramsMain, {
				headers: {
					'Content-Type': 'application/json'
				}
			})
			.then(response => {
				resolve(response)
			})
			.catch(error => {
				reject(error)
			})
	})
}

let taizhouPostApp = (api, params) => {
	let key = 'wNsirOBq87'
	let hostList = [
		baseHost + 'taizhouAppApi/',
		baseHost + 'taizhouAppApi/gatewayAPI/'
	]
	let sid = '7864A7EA7912EA0BE0500B0A0B514ECB'
	return taizhouPost(api, params, hostList, key, sid)
}

//  -------------------------------------  接口

let register = params => {
	return taizhouPostApp('app/user/register', params)
}

let checkRegister = params => {
	return taizhouPostApp('app/ktbaseUser/selectPhoneLogin', params)
}

let forget = params => {
	return taizhouPostApp('app/user/forget', params)
}

let checkPersonInfo = params => {
	return taizhouPostApp('appext/familyRole/IsPerfect', params)
}

let getCardList = params => {
	return taizhouPostApp('appext/familyRole/selectCardListV2', params)
}

let selectFamily = params => {
	return taizhouPostApp('appext/familyRole/selectFamily', params)
}

let getProvinceFace = params => {
	return taizhouPostApp('appext/idInfo/getIDImag', params)
}

let oneFaceCompare = params => {
	return taizhouPostApp('appext/face/comparisonIDInfo', params)
}

let twoFacesCompare = params => {
	return taizhouPostApp('appext/face/comparison', params)
}

let savePhoto = params => {
	return taizhouPostApp('appext/face/saveOcpPhotoNegatives', params)
}

let insertFamily = params => {
	return taizhouPostApp('appext/familyRole/insertFamily', params)
}

let isExist = params => {
	return taizhouPostApp('appext/familyRole/isExist', params)
}

let isBindME = params => {
	return taizhouPostApp('appext/familyRole/isBindME', params)
}

let getHospital = params => {
	return taizhouPostApp('app/ktsysOrganization/selectORGCondition', params)
}

export default {
	register, // 注册接口
	checkRegister, //判断是否注册
	forget, //忘记密码
	checkPersonInfo, //判断本人的个人信息是否完善
	getCardList, //获取家庭所有卡列表
	selectFamily, //获取家庭角色
	getProvinceFace, //获取省厅人脸
	oneFaceCompare, //单张人脸比对
	twoFacesCompare, //两张人脸比对
	savePhoto, //人脸底库照片存入
	insertFamily, //创建家庭角色
	isExist, //判断身份证对应的用户是否已经存在
	isBindME, //判断是否已经被他人绑定过为本人
	getHospital //获取医院列表
}
